package com.yang.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yang.demo.common.Result;
import com.yang.demo.entity.Menu;
import com.yang.demo.entity.User;
import com.yang.demo.mapper.UserMapper;
import com.yang.demo.service.MenuService;
import com.yang.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

//一般只和前端打交道

@RestController  //代表这个类是个控制器
@RequestMapping("/user")  //代表请求的部分路径
public class UserController {

    @Resource
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserMapper userMapper;

    //按id查询
    @GetMapping("/get/{id}")   //整体是个路径  id为参数
    public User getUser(@PathVariable("id") Integer id) {
        return userService.getUser(id);
    }

    @GetMapping("/findByAccount")
    public Result findByAccount(@RequestParam Integer account){
       List list =  userService.lambdaQuery().eq(User::getAccount,account).list();
       return list.size()>0?Result.suc(list):Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        //userService.changeUser(user);
        return userService.save(user) ? Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)? Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return userService.removeById(id)? Result.suc():Result.fail();
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List list = userService.lambdaQuery()
                .eq(User::getAccount,user.getAccount())
                .eq(User::getPassword,user.getPassword()).list();
        if(list.size()>0){
            User user1 = (User)list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuright,user1.getRoleId()).list();
            HashMap res = new HashMap();
            res.put("user",user1);
            res.put("menu",menuList);
            return Result.suc(res);
        }
        return Result.fail();

    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增与修改
    @PostMapping("/saveormod")
    public boolean saveormod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }
    //模糊查询
    @PostMapping("/listp")
    public List<User> listp( @RequestBody User user){
        LambdaQueryWrapper<User>  lambdaQueryWrapper = new LambdaQueryWrapper<>();
//      lambdaQueryWrapper.like(User::getUsername,user.getUsername());//模糊查询
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());//精确查询
        return userService.list(lambdaQueryWrapper);
    }

}
